package com.sfh.cp.user.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;

import com.sfh.cp.R;
import com.sfh.cp.base.AbstractBaseActivity;
import com.sfh.cp.user.ReqLogin;
import com.sfh.cp.user.pst.IPstLogin;
import com.sfh.lib.mvp.IPresenter;
import com.sfh.lib.mvp.service.LoaderService;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 功能描述:登录
 *
 * @date 2018/3/12
 */
public class UserLoginActivity extends AbstractBaseActivity implements IPstLogin.IVLogin {

    public static void startUI(Context context) {
        Intent intent = new Intent(context, UserLoginActivity.class);
        context.startActivity(intent);
    }

    @BindView(R.id.etName)
    EditText etName;
    @BindView(R.id.etPwd)
    EditText etPwd;

    IPstLogin pstLogin;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_login_activity);
        ButterKnife.bind(this);

    }


    @Override
    public IPresenter getPresenter() {
        if (pstLogin == null) {
            pstLogin = LoaderService.getService(IPstLogin.class);
        }
        return pstLogin;
    }

    @Override
    public void onLoginSuccess() {
        finish();
    }

    @OnClick({R.id.btnLogin, R.id.btnRegister})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btnLogin:
                if (pstLogin == null){
                    return;
                }
                ReqLogin req = new ReqLogin();
                req.setPassword(etPwd.getText().toString());
                req.setPhone(etName.getText().toString());

                pstLogin.login(req);
                break;
            case R.id.btnRegister:
                UserRegisterActivity.startUI(this);
                finish();
                break;
        }
    }
}
